package com.hmsm.sys.action;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.servlet.ModelAndView;

import com.hmsm.server.base.BaseAction;
import com.hmsm.server.util.QueryResult;
import com.hmsm.sys.bean.LocalDept;
import com.hmsm.sys.bean.LocalUser;
import com.hmsm.sys.controller.RoleController;
import com.hmsm.sys.po.FuncInfo;
import com.hmsm.sys.po.RoleInfo;

@SuppressWarnings("rawtypes")
public class RoleInfoAction extends BaseAction {
	
	private static final long serialVersionUID = -1648487317945252292L;
	
	@Resource
	private RoleController roleController;
	
	private String nowpage = null;// 页码
	private String maxpage = null;// 每页显示条数	
	private int page;//当前页
	private int rowsPage;//每页数量
	private QueryResult<RoleInfo> result;
	
	private String name;
	private int state;
	private String remark;
	private int rid;
	private RoleInfo roleInfo;
	private Integer orgId;
	private LinkedHashMap<FuncInfo,List<FuncInfo>> menuinfo;
	
	private String managedFuncStr;
	private String funcStr;//功能ID字符
	
	private List<RoleInfo> roleList;//角色列表
	private int userId;//用户编号
	private String roleIds;//角色ID集

	private List<LocalDept> managedOrgList;

	public List<LocalDept> getManagedOrgList() {
		return managedOrgList;
	}

	public void setManagedOrgList(List<LocalDept> managedOrgList) {
		this.managedOrgList = managedOrgList;
	}

	public Integer getOrgId() {
		return orgId;
	}

	public void setOrgId(Integer orgId) {
		this.orgId = orgId;
	}

	public String getManagedFuncStr() {
		return managedFuncStr;
	}

	public void setManagedFuncStr(String managedFuncStr) {
		this.managedFuncStr = managedFuncStr;
	}

	public String getFuncStr() {
		return funcStr;
	}

	public void setFuncStr(String funcStr) {
		this.funcStr = funcStr;
	}
	public String getNowpage() {
		return nowpage;
	}
	public void setNowpage(String nowpage) {
		this.nowpage = nowpage;
	}
	public String getMaxpage() {
		return maxpage;
	}
	public void setMaxpage(String maxpage) {
		this.maxpage = maxpage;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRowsPage() {
		return rowsPage;
	}
	public void setRowsPage(int rowsPage) {
		this.rowsPage = rowsPage;
	}
	public QueryResult<RoleInfo> getResult() {
		return result;
	}
	public void setResult(QueryResult<RoleInfo> result) {
		this.result = result;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}

	public int getRid() {
		return rid;
	}

	public void setRid(int rid) {
		this.rid = rid;
	}

	public RoleInfo getRoleInfo() {
		return roleInfo;
	}

	public void setRoleInfo(RoleInfo roleInfo) {
		this.roleInfo = roleInfo;
	}

	
	
	public LinkedHashMap<FuncInfo, List<FuncInfo>> getMenuinfo() {
		return menuinfo;
	}

	public void setMenuinfo(LinkedHashMap<FuncInfo, List<FuncInfo>> menuinfo) {
		this.menuinfo = menuinfo;
	}

	public List<RoleInfo> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<RoleInfo> roleList) {
		this.roleList = roleList;
	}
	
	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}
	
	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}
	
	private boolean isAdmin = false;

	public boolean isAdmin() {
		return isAdmin;
	}

	public void setAdmin(boolean isAdmin) {
		this.isAdmin = isAdmin;
	}

	/**
	 * 查询角色信息，并进行分页
	 * @return
	 */	
	public String manage() {
		LocalUser currentUser = getSessionUser();
		if(currentUser==null){
			return ERROR;
		}
		try {
			ModelAndView modelAndView = roleController.manage(orgId, name, nowpage, maxpage, currentUser, getRequest());
			if(SUCCESS.equals(modelAndView.getViewName())){
				result = (QueryResult<RoleInfo>) modelAndView.getModel().get("result");
				Map<Integer, Boolean> editMap = (Map<Integer, Boolean>) modelAndView.getModel().get("Edit");
				Map<Integer, Boolean> userAuthMap = (Map<Integer, Boolean>) modelAndView.getModel().get("UserAuth");
				mapResult.put("Edit", editMap);
				mapResult.put("UserAuth", userAuthMap);
				managedOrgList = (List<LocalDept>) modelAndView.getModel().get(RoleController.MANAGED_ORG_LIST);
			}
		} catch (Exception e) {
			e.printStackTrace();
			message = "发生错误："+e;
			addActionError(message);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 角色新增初化始
	 * @return
	 */
	public String add(){
		LocalUser currentUser = getSessionUser();
		if(currentUser==null){
			return ERROR;
		}
		ModelAndView modelAndView;
		try {
			modelAndView = roleController.add(orgId, currentUser, getRequest());
		} catch (Exception e) {
			modelAndView = new ModelAndView(ERROR);
			modelAndView.getModel().put("errorMessage", "发生错误："+e);
			e.printStackTrace();
		}
		if(SUCCESS.equals(modelAndView.getViewName())){
			roleInfo = (RoleInfo) modelAndView.getModel().get("role");
			boolean isAdmin = (Boolean) modelAndView.getModel().get("isAdmin");
			this.rid = roleInfo.getPkId();
			this.orgId = roleInfo.getOrgId();
			this.name = roleInfo.getName();
			this.remark = roleInfo.getRemark();
			this.state = roleInfo.getState();
			managedOrgList = (List<LocalDept>) modelAndView.getModel().get(RoleController.MANAGED_ORG_LIST);
			menuinfo = (LinkedHashMap<FuncInfo, List<FuncInfo>>) modelAndView.getModel().get("rightMap");
			//获取当前角色所对应的所有功能项编号
			funcStr = (String) modelAndView.getModel().get("funcStr");
			managedFuncStr = (String) modelAndView.getModel().get("managedFuncStr");
		}
		definedResult = "edit";
		return "definedResult";
	}

	/**
	 * 初始化角色功能项配置，查询角色名称及菜单信息
	 * @return
	 */
	public String edit(){
		LocalUser currentUser = getSessionUser();//获取登录用户
		if(currentUser == null){
			return ERROR;
		}
		ModelAndView modelAndView = roleController.edit(rid, currentUser, getRequest());
		if(SUCCESS.equals(modelAndView.getViewName())){
			roleInfo = (RoleInfo) modelAndView.getModel().get("role");
			boolean isAdmin = (Boolean) modelAndView.getModel().get("isAdmin");
			this.rid = roleInfo.getPkId();
			this.orgId = roleInfo.getOrgId();
			this.name = roleInfo.getName();
			this.remark = roleInfo.getRemark();
			this.state = roleInfo.getState();
			menuinfo = (LinkedHashMap<FuncInfo, List<FuncInfo>>) modelAndView.getModel().get("rightMap");
			//获取当前角色所对应的所有功能项编号
			funcStr = (String) modelAndView.getModel().get("funcStr");
			managedFuncStr = (String) modelAndView.getModel().get("managedFuncStr");
	
			managedOrgList = (List<LocalDept>) modelAndView.getModel().get(RoleController.MANAGED_ORG_LIST);
			mapResult.put("isAdmin", isAdmin);
			
			List<LocalUser> userList = (List<LocalUser>) modelAndView.getModel().get("userList");
			mapResult.put("roleUserList", userList);
			
		}else{
			message = (String) modelAndView.getModel().get("errorMessage");
			addActionError(message);
		}
		
		return modelAndView.getViewName();
	}

	/**
	 * 保存角色,包括功能项信息
	 * @return
	 * @author 舒俊
	 */
	public String saveRoleAndFunc(){
		LocalUser currentUser = getSessionUser();//获取登录用户
		List<Integer> funcInfoIds = new ArrayList<Integer>();
		if(!"".equals(funcStr)){
			String[] funcIds = funcStr.split(",");
			for(String idStr:funcIds){
				if(idStr.length()>0){
					Integer id = Integer.parseInt(idStr);
					if(id!=null&&id>0){
						funcInfoIds.add(id);
					}
				}
			}
		}
		
		ModelAndView modelAndView = roleController.saveRoleFunc(rid, name, remark, orgId, funcInfoIds, currentUser, getRequest());
		boolean success = (Boolean) modelAndView.getModel().get("success");
		if(success){
			mapResult.put("success", true);
		}else{
			String errorMessage = (String) modelAndView.getModel().get("errorMessage");
			mapResult.put("success", false);
			mapResult.put("errorMessage", errorMessage);
		}
		
		return "json";
	}

	/**
	 * 保存角色
	 * @return
	 */
	public String save(){
		LocalUser currentUser = getSessionUser();//获取登录用户
		
		RoleInfo role = new RoleInfo();
		if (rid>0){
			role.setPkId(rid);
		}
		if(name==null){
			this.setMessage("角色名称不能为空！");
			return ERROR;
		}
		role.setOrgId(orgId);//管理员选择传递过来的机构ID
		role.setName(name);
		role.setRemark(remark);
		role.setState(1);//1有效 2无效
		try {
			ModelAndView modelAndView = roleController.save(role, currentUser, getRequest());//保存角色
			role = (RoleInfo) modelAndView.getModel().get("role");
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("发生错误："+e);
			return ERROR;
		}
		definedResult = "manage";
		return "definedAction";
	}		

	/**
	 * 删除角色
	 * @return
	 */
	public String del(){
		if (rid==0){
			this.addActionError("未指定要删除角色！");
			return ERROR;
		}
		ModelAndView modelAndView = roleController.del(rid, getSessionUser(), getRequest());
		String viewName = modelAndView.getViewName();
		if(!SUCCESS.equals(viewName)){
			message = (String) modelAndView.getModel().get("errorMessage");
			addActionError(message);
			return ERROR;
		}
		
		definedResult = "manage";
		return "definedAction";
	}		
	
	public String resetFuncAndRoles(){
		try {
			getRoleInfoService().resetFuncAndRoles();
			mapResult.put("success", true);
		} catch (Exception e) {
			mapResult.put("success", false);
			mapResult.put("errorMessage", e.getMessage());
			e.printStackTrace();
		}
		return "json";
	}
}
